﻿using Acme.SmartWMS.BasicInfo.SupplierService;
using Acme.SmartWMS.BasicInfo.SupplierService.DTO;
using Acme.SmartWMS.DataBasic;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Acme.SmartWMS.Web.Controllers.BasicInfo
{
    public class SupplierController : Controller
    {
        /// <summary>
        /// 基础资料-供应商管理服务类
        /// </summary>
        private readonly ISupplierAppService _supplierAppService;
        public SupplierController(ISupplierAppService supplierAppService)
        {
            _supplierAppService = supplierAppService;
        }

        public IActionResult SupplierList()
        {
            return View();
        }
        public IActionResult Add()
        {
            return View();
        }
        public IActionResult Update(int id)
        {
            ViewBag.Id = id;    
            return View();
        }
        /// <summary>
        /// 获取供应商管理显示 分页 查询
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="name"></param>
        /// <param name="supplierCode"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetSupplierList(int page, int limit, string? name, string? supplierCode)
        {
            var supplier = await _supplierAppService.GetSupplierList(page, limit, name, supplierCode);
            return Json(new { code = 0, data = supplier.Queryable, count = supplier.RowCount });
        }
        /// <summary>
        /// 获取字典表数据，绑定企业属性下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Task<List<DictItem>> GetDictItems(int dictTypeId)
        {
            return _supplierAppService.GetDictItems(dictTypeId);
        }
        /// <summary>
        /// 添加供应商管理
        /// </summary>
        /// <param name="supplierDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> InsertSupplier(SupplierDTO supplierDTO)
        {
            return await _supplierAppService.InsertSupplier(supplierDTO);
        }
        /// <summary>
        /// 删除供应商管理
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<int> DeleteSupplier(string ids)
        {
            return _supplierAppService.DeleteSupplier(ids);
        }
        /// <summary>
        /// 获取供应商主键id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public Task<SupplierDTO> GetSupplierById(int id)
        {
            ViewBag.id = id;
            return _supplierAppService.GetSupplierById(id);
        }
        /// <summary>
        /// 更新供应商
        /// </summary>
        /// <param name="supplierDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public Task<int> UpdateSupplier(SupplierDTO supplierDTO)
        {
            return _supplierAppService.UpdateSupplier(supplierDTO);
        }
    }
}
